<template>
  <el-dialog
      top="10vh"
      width="800px"
      title="添加/编辑配置"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'ConfigAdd',
  data() {
    return {
      apiKey: 'config',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        key: '',
        title: '',
        value: '',
        description: '',
        type: 'text',
        sort: 0,
        status: 1
      },
      rules: {
        key: [{required: true, message: '配置键名不能为空'}],
        title: [{required: true, message: '配置标题不能为空'}],
        value: [{required: true, message: '配置值不能为空'}],
        type: [{required: true, message: '请选择配置类型'}],
      },
      formList: [
        {
          label: '配置键名',
          prop: 'key',
          name: 'input',
          placeholder: '请输入配置键名（如：customer_service_qr）',
          colSpan: 12
        },
        {
          label: '配置标题',
          prop: 'title',
          name: 'input',
          placeholder: '请输入配置标题',
          colSpan: 12
        },
        {
          label: '配置类型',
          prop: 'type',
          name: 'select',
          placeholder: '请选择配置类型',
          colSpan: 12,
          options: [
            { label: '文本', value: 'text' },
            { label: '图片', value: 'image' },
            { label: '多行文本', value: 'textarea' }
          ]
        },
        {
          label: '配置描述',
          prop: 'description',
          name: 'textarea',
          placeholder: '请输入配置描述（可选）',
          colSpan: 12,
          rows: 2
        },
        {
          label: '配置值',
          prop: 'value',
          name: 'input',
          type: '',
          placeholder: '请输入配置值',
          colSpan: 24
        },
        {
          label: '排序',
          prop: 'sort',
          name: 'number',
          placeholder: '请输入排序值',
          colSpan: 12,
          min: 0,
          max: 999
        },
        {
          label: '状态',
          prop: 'status',
          name: 'radio',
          colSpan: 12,
          options: [
            { label: '启用', value: 1 },
            { label: '禁用', value: 0 }
          ]
        }
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // 当配置类型改变时，动态调整配置值的输入方式
      if (key === 'type') {
        const valueFormItem = this.formList.find(item => item.prop === 'value');
        if (val === 'text') {
          valueFormItem.name = 'input';
          valueFormItem.placeholder = '请输入配置值';
          valueFormItem.type = 'textarea';
          delete valueFormItem.rows;
          delete valueFormItem.accept;
        } else if (val === 'textarea') {
          valueFormItem.name = 'rich';
          valueFormItem.placeholder = '请输入配置值';
          delete valueFormItem.rows;
          delete valueFormItem.accept;
        } else if (val === 'image') {
          valueFormItem.name = 'file';
          valueFormItem.placeholder = '请选择图片文件';
          valueFormItem.accept = 'image/*';
          delete valueFormItem.rows;
        }
      }
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
          }
          // 根据配置类型设置正确的表单控件
          this.handleFormChange({key: 'type', val: res.data.type});
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
        // 重置为默认的input类型
        const valueFormItem = this.formList.find(item => item.prop === 'value');
        valueFormItem.name = 'input';
        valueFormItem.placeholder = '请输入配置值';
        delete valueFormItem.rows;
        delete valueFormItem.accept;
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
